package com.ztesoft.sca.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Component;

import com.ztesoft.sca.model.RuleModel;

@Component
public class RuleDaoImpl implements RuleDao {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public RuleDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

	@Override
	public List<RuleModel> selectAichatRules(long cId) {
        String sqlStr="select rid from SCA_SCENE_RULE_RELA where scene_id =? ";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{cId};
        List<RuleModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(RuleModel.class));
        return list;
	}

	@Override
	public List<RuleModel> selectDataRuleList() {
        String sqlStr="select rid,bid,rule_name,status,auto_review,rule_score_type,score_name,score_subname,comments,create_time,isdelete from SCA_RULE";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{};
        List<RuleModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(RuleModel.class));
        return list;
	}

	@Override
	public void addRuleData(List<RuleModel> list) {
    	 NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
	   	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
	   	 String sql = "INSERT INTO SCA_RULE(rid,bid,rule_name,status,auto_review,rule_score_type,score_name,score_subname,comments,create_time,isdelete) "
	   	 		+ "VALUES (:rid,:bid,:ruleName,:status,:autoReview,:ruleScoreType,:scoreName,:scoreSubname,:comments,:createTime,:isDelete)";
	   	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
	}

	@Override
	public void updateRuleData(List<RuleModel> listUpdate) {
	     NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
	   	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(listUpdate.toArray());
	   	 String sql = "UPDATE SCA_RULE SET bid=:bid,rule_name=:ruleName,status=:status,auto_review=:autoReview,rule_score_type=:ruleScoreType,score_name=:scoreName,"
	   	 		+ "score_subname=:scoreSubname,comments=:comments,create_time=:createTime,isdelete=:isDelete where rid=:rid";
	   	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
	}

	@Override
	public List<RuleModel> detailRuleTask(RuleModel aiChat) {
        String sqlStr="select rid,bid,rule_name,status,auto_review,rule_score_type,score_name,score_subname,comments,create_time,isdelete from SCA_RULE where rid=? ";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{aiChat.getRid()};
        List<RuleModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(RuleModel.class));
        return list;
	}
}
